package com.dcy.workflow.service;

import com.dcy.common.model.PageModel;
import com.dcy.common.model.PageResult;
import com.dcy.workflow.dao.ActModelDao;
import com.dcy.workflow.vo.in.ModelCreateInVO;
import com.dcy.workflow.vo.in.ModelSearchInVO;
import com.dcy.workflow.vo.in.ModelUpdateInVO;
import com.dcy.workflow.vo.out.ModelListOutVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.image.ProcessDiagramGenerator;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * @Author：dcy
 * @Description:
 * @Date: 2021/6/7 13:26
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class ActModelService {

    private final ActModelDao actModelDao;
    private final RepositoryService repositoryService;
    private final ProcessEngine processEngine;


    /**
     * 模型分页列表
     *
     * @param modelSearchInVO
     * @param pageModel
     * @return
     */
    public PageResult<ModelListOutVO> modelPageList(ModelSearchInVO modelSearchInVO, PageModel pageModel) {
        return actModelDao.modelPageList(modelSearchInVO, pageModel);
    }

    /**
     * 创建模型
     *
     * @param modelSaveInputDTO
     * @return
     */
    public Boolean createModel(ModelCreateInVO modelSaveInputDTO) {
        return actModelDao.createModel(modelSaveInputDTO.getKey(),
                modelSaveInputDTO.getName(),
                modelSaveInputDTO.getCategory(),
                modelSaveInputDTO.getDescription(),
                modelSaveInputDTO.getJsonXml(),
                modelSaveInputDTO.getSvgXml());
    }

    /**
     * 修改模型
     *
     * @param modelUpdateInVO
     * @return
     */
    public Boolean updateModel(ModelUpdateInVO modelUpdateInVO) {
        return actModelDao.updateModel(modelUpdateInVO.getKey(),
                modelUpdateInVO.getId(),
                modelUpdateInVO.getName(),
                modelUpdateInVO.getCategory(),
                modelUpdateInVO.getDescription(),
                modelUpdateInVO.getJsonXml(),
                modelUpdateInVO.getSvgXml());
    }

    /**
     * 根据模型id查询json
     *
     * @param modelId
     * @return
     */
    public String getModelJsonByModelId(String modelId) {
        return actModelDao.getModelJsonByModelId(modelId);
    }

    /**
     * 部署模型
     *
     * @param modelId
     * @return
     */
    public Boolean deployModel(String modelId) {
        return actModelDao.deployModel(modelId);
    }

    /**
     * 删除模型
     *
     * @param modelId
     * @return
     */
    public Boolean deleteModel(String modelId) {
        actModelDao.deleteModel(modelId);
        return Boolean.TRUE;
    }

    /**
     * 根据流程定义id获取流程图
     *
     * @param processDefinitionId
     */
    public InputStream getBpmnModel(String processDefinitionId) {
        //获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        ProcessEngineConfiguration processEngineConfig = processEngine.getProcessEngineConfiguration();

        ProcessDiagramGenerator diagramGenerator = processEngineConfig.getProcessDiagramGenerator();

        return diagramGenerator.generateDiagram(bpmnModel, "bmp", processEngineConfig.getActivityFontName(),
                processEngineConfig.getLabelFontName(), processEngineConfig.getAnnotationFontName(), processEngineConfig.getClassLoader(), true);
    }

}
